package com.ali.order.service.impl;

import com.ali.item.client.ItemClient;
import com.ali.item.common.DecreaseStockInput;
import com.ali.item.common.ItemInfoOutput;
import com.ali.item.common.ItemSkuOutput;
import com.ali.order.dto.OrderDTO;
import com.ali.order.entity.OrderDetail;
import com.ali.order.entity.OrderMaster;
import com.ali.order.enums.OrderStatusEnum;
import com.ali.order.enums.PayStatusEnum;
import com.ali.order.repository.OrderDetailReponsitory;
import com.ali.order.repository.OrderMasterReponsitory;
import com.ali.order.service.OrderService;
import com.ali.order.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName OrderServiceImpl
 * @Description TODO
 * @Author Alistair.Chow
 * @date 2018/12/21 14:04
 * @Version 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDetailReponsitory orderDetailReponsitory;
    @Autowired
    private OrderMasterReponsitory orderMasterReponsitory;
    @Autowired
    private ItemClient itemClient;

    @Override
    public OrderDTO create(OrderDTO orderDTO) {
        String orderId = KeyUtil.genUniqueKey();
        // 查询商品信息
        List<Integer> itemIdList = orderDTO.getOrderDetailList().stream()
                .map(OrderDetail::getItemId)
                .collect(Collectors.toList());
        List<ItemInfoOutput> itemInfoList = itemClient.itemListForOrder(itemIdList);

        // 查询库存信息
        List<Integer> skuIdList = orderDTO.getOrderDetailList().stream()
                .map(OrderDetail::getItemSkuId)
                .collect(Collectors.toList());
        List<ItemSkuOutput> itemSkuList = itemClient.skuListForOrder(skuIdList);

        // 计算总价
        BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO);
        for (OrderDetail orderDetail : orderDTO.getOrderDetailList()){
            for (ItemSkuOutput itemSku : itemSkuList){
                if(itemSku.getId().equals(orderDetail.getItemSkuId())){
                    // 单价*数量
                    orderAmount = itemSku.getPrice()
                    .multiply(new BigDecimal(orderDetail.getItemQuantity()))
                    .add(orderAmount);
                    BeanUtils.copyProperties(itemSku, orderDetail);
                    orderDetail.setItemSkuName(itemSku.getName());
                    orderDetail.setItemPrice(itemSku.getPrice());
                    orderDetail.setItemPic(itemSku.getPicture());
                    orderDetail.setOrderId(orderId);
                    orderDetail.setId(KeyUtil.genUniqueKey());
                    // 订单详情入库
                    orderDetailReponsitory.save(orderDetail);
                }
            }

        }

        // 扣库存(调用商品服务)
        List<DecreaseStockInput> decreaseStockInputList = orderDTO.getOrderDetailList().stream()
                .map(e -> new DecreaseStockInput(e.getItemId(), e.getItemSkuId(), e.getItemQuantity()))
                .collect(Collectors.toList());
        itemClient.decreaseStock(decreaseStockInputList);


        // 订单入库
        OrderMaster orderMaster = new OrderMaster();
        orderDTO.setId(orderId);
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMaster.setOrderAmount(orderAmount);
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
        orderMasterReponsitory.save(orderMaster);
        return orderDTO;
    }
}
